﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace gt.IdentityServer.Migrations
{
    public partial class InitialCreate_IdentityModel : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "t_app_group",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(450)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_t_app_group", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "t_app_role",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(450)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_t_app_role", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "t_app_user",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserName = table.Column<string>(type: "nvarchar(450)", nullable: true),
                    Password = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    ShowName = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Age = table.Column<int>(type: "int", nullable: false),
                    Gender = table.Column<int>(type: "int", nullable: false),
                    Phone = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Introduce = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    RegsterTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    GroupId = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_t_app_user", x => x.Id);
                    table.ForeignKey(
                        name: "FK_t_app_user_t_app_group_GroupId",
                        column: x => x.GroupId,
                        principalTable: "t_app_group",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "t_app_grouprole_mapping",
                columns: table => new
                {
                    GroupId = table.Column<int>(type: "int", nullable: false),
                    RoleId = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_t_app_grouprole_mapping", x => new { x.GroupId, x.RoleId });
                    table.ForeignKey(
                        name: "FK_t_app_grouprole_mapping_t_app_group_GroupId",
                        column: x => x.GroupId,
                        principalTable: "t_app_group",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_t_app_grouprole_mapping_t_app_role_RoleId",
                        column: x => x.RoleId,
                        principalTable: "t_app_role",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "t_app_userrole_mapping",
                columns: table => new
                {
                    UserId = table.Column<int>(type: "int", nullable: false),
                    RoleId = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_t_app_userrole_mapping", x => new { x.UserId, x.RoleId });
                    table.ForeignKey(
                        name: "FK_t_app_userrole_mapping_t_app_role_RoleId",
                        column: x => x.RoleId,
                        principalTable: "t_app_role",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_t_app_userrole_mapping_t_app_user_UserId",
                        column: x => x.UserId,
                        principalTable: "t_app_user",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_t_app_group_Name",
                table: "t_app_group",
                column: "Name",
                unique: true,
                filter: "[Name] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_t_app_grouprole_mapping_RoleId",
                table: "t_app_grouprole_mapping",
                column: "RoleId");

            migrationBuilder.CreateIndex(
                name: "IX_t_app_role_Name",
                table: "t_app_role",
                column: "Name",
                unique: true,
                filter: "[Name] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_t_app_user_GroupId",
                table: "t_app_user",
                column: "GroupId");

            migrationBuilder.CreateIndex(
                name: "IX_t_app_user_UserName",
                table: "t_app_user",
                column: "UserName",
                unique: true,
                filter: "[UserName] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_t_app_userrole_mapping_RoleId",
                table: "t_app_userrole_mapping",
                column: "RoleId");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "t_app_grouprole_mapping");

            migrationBuilder.DropTable(
                name: "t_app_userrole_mapping");

            migrationBuilder.DropTable(
                name: "t_app_role");

            migrationBuilder.DropTable(
                name: "t_app_user");

            migrationBuilder.DropTable(
                name: "t_app_group");
        }
    }
}
